import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;


public class ParseScriptsForBeehive {

	private final static String inputDirName = "..\\Beehive\\Results";
	private final static String outputDirName = "..\\Beehive\\Parsed_Results";
	/**
	 * @param args
	 */
	public static void parse(String fileName, int numberOfNodes) {
		
		File dir = new File(outputDirName);
		// if the directory does not exist, create it
		if (!dir.exists())
			dir.mkdir();  
		
		for (int i = 0; i < numberOfNodes; i++){
			try {
				
				File file = new File(inputDirName + "\\" + fileName + i +".txt");
				Scanner input = new Scanner(file);
				
				String lookup = "";
				String objectsInStorage = "";
				String maxObjectsInStorage = "";
				String replicationMessage = "";
				String objectsToDelete = "";
				
				while(input.hasNext()) {
				    String nextLine = input.nextLine();
				    if (nextLine.contains("Average_lookup_for_value:")){
				    	nextLine = nextLine.replace("Average_lookup_for_value:", "");
				    	nextLine = nextLine.replace(" ", "");
				    	lookup += nextLine +",";
				    } else if (nextLine.contains("Avarage_number_of_objects_in_storage:")){
				    	nextLine = nextLine.replace("Avarage_number_of_objects_in_storage:", "");
				    	nextLine = nextLine.replace(" ", "");
				    	objectsInStorage += nextLine +",";
				    } else if (nextLine.contains("Max_objects_in_storage:")){
				    	nextLine = nextLine.replace("Max_objects_in_storage:", "");
				    	nextLine = nextLine.replace(" ", "");
				    	maxObjectsInStorage += nextLine +",";
				    } else if (nextLine.contains("Number_of_replication_message:")){
				    	nextLine = nextLine.replace("Number_of_replication_message:", "");
				    	nextLine = nextLine.replace(" ", "");
				    	nextLine = replicationMessage += nextLine +",";
				    } else if (nextLine.contains("Avarage_number_of_objects_B_suggests_to_delete:")){
				    	nextLine = nextLine.replace("Avarage_number_of_objects_B_suggests_to_delete:", "");
				    	nextLine = nextLine.replace(" ", "");
				    	objectsToDelete += nextLine +",";
				    }
				}
				int lastIndex = lookup.lastIndexOf(",");
				lookup = lookup.substring(0, lastIndex);
				
				lastIndex = objectsInStorage.lastIndexOf(",");
				objectsInStorage = objectsInStorage.substring(0, lastIndex);
	
				lastIndex = maxObjectsInStorage.lastIndexOf(",");
				maxObjectsInStorage = maxObjectsInStorage.substring(0, lastIndex);
				
				lastIndex = replicationMessage.lastIndexOf(",");
				replicationMessage = replicationMessage.substring(0, lastIndex);
				
				lastIndex = objectsToDelete.lastIndexOf(",");
				objectsToDelete = objectsToDelete.substring(0, lastIndex);
				
				FileWriter fw = new FileWriter(outputDirName + "\\" + fileName + "Lookup" + i + ".csv");
				BufferedWriter bw = new BufferedWriter(fw);
				bw.write(lookup);
				bw.close();
				
				fw = new FileWriter(outputDirName + "\\" + fileName + "ObjectsInStorage" + i +".csv");
				bw = new BufferedWriter(fw);
				bw.write(objectsInStorage);
				bw.close();
				
				fw = new FileWriter(outputDirName + "\\" + fileName + "MaxObjectsInStorage" + i + ".csv");
				bw = new BufferedWriter(fw);
				bw.write(maxObjectsInStorage);
				bw.close();
				
				fw = new FileWriter(outputDirName + "\\" + fileName + "ReplicationMessage" + i + ".csv");
				bw = new BufferedWriter(fw);
				bw.write(replicationMessage);
				bw.close();
				
				fw = new FileWriter(outputDirName + "\\" + fileName + "ObjectsToDelete" + i + ".csv");
				bw = new BufferedWriter(fw);
				bw.write(objectsToDelete);
				bw.close();
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
